<template>
  <div class="work-calendar">
    <el-row type="flex" justify="end">
      <el-select v-model="year" size="mini" @change="dataChang">
        <el-option v-for="item in yearList" :key="item" :value="item" />
      </el-select>
      <el-select v-model="month" size="mini" @change="dataChang">
        <el-option v-for="item in 12" :key="item" :value="item" />
      </el-select>
    </el-row>
    <!-- 日历组件 -->
    <el-calendar v-model="curDate">
      <template v-slot:dateCell="{date,data}" class="content">
        <div class="date-content">
          <span class="text">{{ data.day |formatDay }}</span>
          <span v-if="isWeek(date)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>
<script>
const curDate = new Date()// 当前日期
const month = curDate.getMonth() + 1 // 当前日期月份
const year = curDate.getFullYear() // 当前年份
const yearList = []
// 根据当前年份生成前后5年的年份列表
for (let i = 0; i < 11; i++) {
  yearList.push(year - 5 + i)
}
export default {
  filters: { // 过滤器
    formatDay(val) {
      if (!val) { return val }
      return +val.split('-')[2]
    }
  },
  data() {
    return {
      curDate, // 日期
      month, // 月份
      year, // 年份
      yearList// 年份列表
    }
  },
  watch: {
    curDate(date) {
      this.year = date.getFullYear()
      this.month = date.getMonth() + 1
    }
  },
  methods: {
    dataChang() {
      this.curDate = new Date(`${this.year}-${this.month}`)
    },
    isWeek(date) {
      // 根据传入的日期是否为周末, 返回 true / false
      return date.getDay() === 6 || date.getDay() === 0
    }
  }
}
</script>
  <style lang="scss" scoped>
   ::v-deep .el-calendar-day {
    height:  auto;
   }
   ::v-deep .el-calendar-table__row td,::v-deep .el-calendar-table tr td:first-child,  ::v-deep .el-calendar-table__row td.prev{
    border:none;
   }
  .date-content {
    height: 40px;
    text-align: center;
    line-height: 40px;
    font-size: 14px;
  }
  .date-content .rest {
    color: #fff;
    border-radius: 50%;
    background: rgb(250, 124, 77);
    width: 20px;
    height: 20px;
    line-height: 20px;
    display: inline-block;
    font-size: 12px;
    margin-left: 10px;
  }
  .date-content .text{
    width: 20px;
    height: 20px;
    line-height: 20px;
   display: inline-block;

  }
   ::v-deep .el-calendar-table td.is-selected .text{
     background: #409eff;
     color: #fff;
     border-radius: 50%;
   }
   ::v-deep .el-calendar__header {
     display: none
   }
  </style>

